package org.nuxeo.gwt.habyt.upload.demo.client;

import org.nuxeo.gwt.habyt.upload.client.FileRef;
import org.nuxeo.gwt.habyt.upload.client.FileWidgetProvider;
import org.nuxeo.gwt.habyt.upload.client.core.HijackStrategy;
import org.nuxeo.gwt.habyt.upload.client.core.MultiUpload;
import org.nuxeo.gwt.habyt.upload.client.core.SimpleUpload;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Main implements EntryPoint {

    protected Page page;

    static class Page {
        protected String href;

        protected int strategy = 2;

        public Page() {
            String v = Window.Location.getParameter("strategy");
            if (v != null) {
                try {
                    strategy = Integer.parseInt(v);
                } catch (Exception e) {
                    // do nothing
                }
            }
            switch (strategy) {
            case 0:
                HijackStrategy.setBasic();
                break;
            case 1:
                HijackStrategy.setBasic();
                break;
            case 2:
                HijackStrategy.setDefault();
                break;
            case 3:
                HijackStrategy.setHasClick();
                break;
            }
        }

        public boolean hasCustomFileWidget() {
            return strategy > 0;
        }

        public FileWidgetProvider createFileWidgetProvider(String text) {
            return strategy == 0 ? null : new MyFileWidgetProvider(text);
        }

        public String getHref() {
            return Window.Location.createUrlBuilder().setParameter("strategy",
                    String.valueOf(strategy)).buildString();
        }

        public int getStrategy() {
            return strategy;
        }

        public void setStrategy(int strategy) {
            this.strategy = strategy;
        }

        public void reload() {
            Window.Location.replace(getHref());
        }
    }

    static class MyFileWidgetProvider implements FileWidgetProvider {
        protected String text;

        MyFileWidgetProvider(String text) {
            this.text = text;
        }

        @Override
        public Widget getFileWidget() {
            Anchor a = new Anchor(text);
            return a;
        }
    }

    protected FileRef createFile(String id, String name) {
        FileRef fr = new FileRef(id, name);
        fr.setContentType("text/plain");
        fr.setLength("1024");
        return fr;
    }

    protected Widget createFileInputStrategy() {
        final ListBox list = new ListBox();
        HorizontalPanel panel = new HorizontalPanel();
        panel.setSpacing(6);
        panel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        panel.add(new HTML("Input File Strategy: "));
        panel.add(list);
        list.addItem("Basic (use input file widget)");
        list.addItem("Hide");
        list.addItem("Hijack");
        list.addItem("Click Hijack (i.e. Hijack on webkit & ie and Hide on other browsers)");
        list.setSelectedIndex(page.getStrategy());
        list.addChangeHandler(new ChangeHandler() {
            @Override
            public void onChange(ChangeEvent event) {
                page.setStrategy(list.getSelectedIndex());
                page.reload();
            }
        });
        return panel;
    }

    public void onModuleLoad() {
        this.page = new Page();
        MultiUpload mu = new MultiUpload("upload",
                page.createFileWidgetProvider("Add File"));
        // mu.addFile(createFile("f1", "file1.txt"));
        // mu.addFile(createFile("f2", "file2.txt"));
        SimpleUpload su = new SimpleUpload("upload",
                page.createFileWidgetProvider("Choose File"));
        // su.setFile(createFile("f3", "file3.txt"));
        FlowPanel panel = new FlowPanel();
        panel.add(createFileInputStrategy());
        panel.add(new HTML("<h3>Multiple File Upload</h3>"));
        panel.add(mu);
        panel.add(new HTML("<h3>Single File Upload</h3>"));
        panel.add(su);
        RootPanel.get().add(panel);
    }
}
